## This R code replicates Figures 5, 6, 7, A19, A20, A21, A22, A24, A25, A26, A27, 

##setwd("/Users/sparshasaha/Dropbox/Seeking More Why Women Fail/Pol Behavior Rep Files Ambitious Women 02282020")
##setwd("C:/Users/acw64/Dropbox/Seeking More Why Women Fail/Pol Behavior Rep Files Ambitious Women 02282020")

##library(cregg)

################################### DLABSS 1 Survey ###################################
#######################################################################################

mydata2 = read.csv("dlabss_one.csv")
dim(mydata2)

## clean up Experience because Excel changed numbers to dates

newdata <- mydata2

newdata$Experience1 <- as.numeric(newdata$Experience)

## More than 20 = 5 // 10 - 20 =  2 // 5 - 10 = 1 // 5 - 10 = 3 // 0 - 5 = 4

newdata$Experience2[newdata$Experience1 == 5] <- "More than 20"
newdata$Experience2[newdata$Experience1 == 4] <- "0 - 5"
newdata$Experience2[newdata$Experience1 == 3] <- "5 - 10"
newdata$Experience2[newdata$Experience1 == 2] <- "10 - 20"
newdata$Experience2[newdata$Experience1 == 1] <- "5 - 10"

newdata$Experience2 <- as.factor(newdata$Experience2)

## fix levels so they are linear

newdata$Experience2 <- factor(newdata$Experience2, levels = c("0 - 5", "5 - 10", "10 - 20", "More than 20"))

newdata$Experience <- NULL
newdata$Experience <- newdata$Experience2

data <- newdata
dim(data)
head(data)

## change variable name

data$PartyAlt <- factor(data$Party.2)

data$Party3[data$Party==1 | data$PartyAlt==1] <- "Democrat"
data$Party3[data$Party==2 | data$PartyAlt==2] <- "Republican"

data$Party3 <- factor(data$Party3)
nrow(data)

vars <-c("candidate_vote", "Gender", "id", "Progressive", "Agenda", "Experience", "Personalistic", "resp_gender", "Party", "Party3")
mydata <- data[,vars]
nrow(mydata)

## clean this up a bit

mydata$resp_gender2[mydata$resp_gender==1] <- "Male"
mydata$resp_gender2[mydata$resp_gender==2] <- "Female"
mydata$resp_gender2 <- factor(mydata$resp_gender2)


## interactions

mydata$GenderxProgressive <- with(mydata, interaction(Gender, Progressive), drop = TRUE)
mydata$GenderxAgenda <- with(mydata, interaction(Gender, Agenda), drop = TRUE)
mydata$GenderxExperience <- with(mydata, interaction(Gender, Experience), drop = TRUE)
mydata$GenderxPersonalistic <- with(mydata, interaction(Gender, Personalistic), drop = TRUE)

## Code below produces Figure A25 (Gender of Respondent)

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxExperience + GenderxPersonalistic + GenderxAgenda +�
GenderxProgressive, id = ~id, estimate = "mm", by = ~resp_gender2)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxExperience + GenderxPersonalistic + GenderxAgenda +�
GenderxProgressive, id = ~id, estimate = "mm_diff", by = ~resp_gender2)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

write.csv(mms, file = "DLABSS1_Gender_Marginals_mms.csv")

write.csv(diff_mms, file = "DLABSS1_Gender_Marginals_diff_mms.csv")


## Code below produces Figure A21 (Party of Respondent)

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxExperience + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm", by = ~Party3)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxExperience + GenderxPersonalistic + GenderxAgenda +
GenderxProgressive, id = ~id, estimate = "mm_diff",� by = ~Party3)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

write.csv(mms, file = "DLABSS1_Party_Marginals_mms.csv")

write.csv(diff_mms, file = "DLABSS1_Party_Marginals_diff_mms.csv")


################################### DLABSS 2 Survey ###################################
#######################################################################################

mydata2 = read.csv("dlabss_second.csv")
dim(mydata2)

data <- mydata2

# change variable name

data$PartyAlt <- factor(data$Party.2)

data$Party3[data$Party==1 | data$PartyAlt==1] <- "Democrat"
data$Party3[data$Party==2 | data$PartyAlt==2] <- "Republican"
data$Party3 <- factor(data$Party3)


vars <-c("candidate_vote", "Gender", "id", "Progressive", "Agenda", "Experience", "Personalistic", "Job", "Age", "resp_gender", "Party", "Party3")
mydata <- data[,vars]
nrow(mydata)


## clean this up a bit

mydata$resp_gender2[mydata$resp_gender==1] <- "Male"
mydata$resp_gender2[mydata$resp_gender==2] <- "Female"

mydata$resp_gender2 <- factor(mydata$resp_gender2)

## interactions

mydata$GenderxProgressive <- with(mydata, interaction(Gender, Progressive), drop = TRUE)
mydata$GenderxAgenda <- with(mydata, interaction(Gender, Agenda), drop = TRUE)
mydata$GenderxExperience <- with(mydata, interaction(Gender, Experience), drop = TRUE)
mydata$GenderxPersonalistic <- with(mydata, interaction(Gender, Personalistic), drop = TRUE)
mydata$GenderxJob <- with(mydata, interaction(Gender, Job), drop = TRUE)
mydata$GenderxAge <- with(mydata, interaction(Gender, Age), drop = TRUE)


## Code below produces Figure A26 (Gender of Respondent)

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxExperience + GenderxPersonalistic + GenderxAgenda +�
GenderxProgressive + GenderxJob + GenderxAge, id = ~id, estimate = "mm", by = ~resp_gender2)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxExperience + GenderxPersonalistic + GenderxAgenda +�
GenderxProgressive + GenderxJob + GenderxAge, id = ~id, estimate = "mm_diff",�
� by = ~resp_gender2)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

write.csv(mms, file = "DLABSS2_Gender_Marginals_mms.csv")

write.csv(diff_mms, file = "DLABSS2_Gender_Marginals_diff_mms.csv")


## Code below produces Figure A22 (Party of Respondent)

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxExperience + GenderxPersonalistic + GenderxAgenda +�
GenderxProgressive + GenderxJob + GenderxAge, id = ~id, estimate = "mm", by = ~Party3)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxExperience + GenderxPersonalistic + GenderxAgenda +�
GenderxProgressive + GenderxJob + GenderxAge, id = ~id, estimate = "mm_diff",�
 by = ~Party3)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

write.csv(mms, file = "DLABSS2_Party_Marginals_mms.csv")

write.csv(diff_mms, file = "DLABSS2_Party_Marginals_diff_mms.csv")


################################### SSI Survey ######################################
#####################################################################################

mydata2 = read.csv("ssi.csv")
dim(mydata2)
data <- mydata2

## create Party4 variable

data$Party2 <- factor(data$Interests.Political.Affiliation)
data$Party2 <- ifelse(data$Interests.Political.Affiliation == "Democrat" | data$Interests.Political.Affiliation == "Republican (GOP)", data$Party2, 0)
data$Party2[data$Party2 == 0] <- NA
data$Party3 <- factor(data$Party2)
data$Party4 <- ifelse(data$Party3=="5", "Republican", data$Party3)
data$Party4 <- ifelse(data$Party3=="2", "Democrat", data$Party4)
data$Party4 <- factor(data$Party4)

## 5 = republican, 2 = democrat

vars <-c("candidate_vote", "Gender", "id", "Progressive", "Agenda", "Children", "Personalistic", "resp_gender", "Party4")
mydata <- data[,vars]
nrow(mydata)

## clean this up a bit

mydata$resp_gender2 <- factor(mydata$resp_gender)

## interactions

mydata$GenderxProgressive <- with(mydata, interaction(Gender, Progressive), drop = TRUE)
mydata$GenderxAgenda <- with(mydata, interaction(Gender, Agenda), drop = TRUE)
mydata$GenderxChildren <- with(mydata, interaction(Gender, Children), drop = TRUE)
mydata$GenderxPersonalistic <- with(mydata, interaction(Gender, Personalistic), drop = TRUE)

## Code below produces Figure 7

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + GenderxProgressive, id = ~id, estimate = "mm", by = ~resp_gender2)

mms <- mms[-c(1:10, 17, 18, 19, 20, 25, 26, 33:42, 49:52, 57, 58),]

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + GenderxProgressive, id = ~id, estimate = "mm_diff", by = ~resp_gender2)

diff_mms <- diff_mms[-c(1:10, 17:20, 25, 26),]

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

## Code below produces Appendix Figure A24

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + GenderxProgressive, id = ~id, estimate = "mm", by = ~resp_gender2)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + GenderxProgressive, id = ~id, estimate = "mm_diff", by = ~resp_gender2)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

## actual estimates

write.csv(mms, file = "SSI_Gender_Marginals_mms.csv")

write.csv(diff_mms, file = "SSI_Gender_Marginals_diff_mms.csv")

## Code below produces Figure 5

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + GenderxProgressive, id = ~id, estimate = "mm", by = ~Party4)

mms <- mms[-c(1:10, 17, 18, 19, 20, 25, 26, 33:42, 49:52, 57, 58),]

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + GenderxProgressive, id = ~id, estimate = "mm_diff", by = ~Party4)

diff_mms <- diff_mms[-c(1:10, 17:20, 25, 26),]

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

## Code below produces Appendix Figure A19

mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + GenderxProgressive, id = ~id, estimate = "mm", by = ~Party4)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ GenderxChildren + GenderxPersonalistic + GenderxAgenda + GenderxProgressive, id = ~id, estimate = "mm_diff", by = ~Party4)

plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

## actual estimates

write.csv(mms, file = "SSI_Party_Marginals_mms.csv")

write.csv(diff_mms, file = "SSI_Party_Marginals_diff_mms.csv")

################################### Prolific Survey ######################################
##########################################################################################

mydata2 = read.csv("prolific.csv")
dim(mydata2)
head(mydata2)

## create the interaction vars 

mydata2$GenderxProgressive <- with(mydata2, interaction(Gender,  Progressive), drop = TRUE )
mydata2$GenderxAgenda <- with(mydata2, interaction(Gender,  Agenda), drop = TRUE )
mydata2$GenderxParental <- with(mydata2, interaction(Gender, Parental), drop = TRUE )
mydata2$GenderxPersonalistic <- with(mydata2, interaction(Gender, Personalistic), drop = TRUE )

## Code below produces Figure A27

mms <- cj(na.omit(mydata2), candidate_vote ~ GenderxParental + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm", by = ~RespondentGender)
diff_mms <- cj(na.omit(mydata2), candidate_vote ~ GenderxParental + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm_diff", 
    by = ~RespondentGender)
plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

## Code below produces Figure 6

mms <- cj(na.omit(mydata2), candidate_vote ~ GenderxParental + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm", by = ~RespondentParty)

mms <- mms[-c(1:10, 17, 18, 19, 20, 25, 26, 33:42, 49:52, 57, 58, 65:74, 81:84, 89, 90),]

diff_mms <- cj(na.omit(mydata2), candidate_vote ~ GenderxParental + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm_diff", 
    by = ~RespondentParty)
    
diff_mms <- diff_mms[-c(1:10, 17, 18, 19, 20, 25, 26, 33:42, 49:52, 57, 58),]
    
plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)

## plot(diff_mms)+ ggplot2::facet_wrap(~BY, ncol = 3L)

## Code below produces Appendix Figure A20

mms <- cj(na.omit(mydata2), candidate_vote ~ GenderxParental + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm", by = ~RespondentParty)
diff_mms <- cj(na.omit(mydata2), candidate_vote ~ GenderxParental + GenderxPersonalistic + GenderxAgenda + 
GenderxProgressive, id = ~id, estimate = "mm_diff", 
    by = ~RespondentParty)
plot(rbind(mms, diff_mms)) + ggplot2::facet_wrap(~BY, ncol = 3L)
plot(diff_mms)+ ggplot2::facet_wrap(~BY, ncol = 3L)

############Robustness check for main finding on progressive ambition ####################
##########################################################################################

mydata2 = read.csv("ssi.csv")
dim(mydata2)

data <- mydata2

## create Party4 variable

data$Party2 <- factor(data$Interests.Political.Affiliation)
data$Party2 <- ifelse(data$Interests.Political.Affiliation == "Democrat" | data$Interests.Political.Affiliation == "Republican (GOP)", data$Party2, 0)
data$Party2[data$Party2 == 0] <- NA
data$Party3 <- factor(data$Party2)
data$Party4 <- ifelse(data$Party3=="5", "Republican", data$Party3)
data$Party4 <- ifelse(data$Party3=="2", "Democrat", data$Party4)
data$Party4 <- factor(data$Party4)

## 5 = republican, 2 = democrat

vars <-c("candidate_vote", "Gender", "id", "Progressive", "Agenda", "Children", "Personalistic", "resp_gender", "Party4")
mydata <- data[,vars]
nrow(mydata)

mms <- cj(na.omit(mydata), candidate_vote ~ Gender, id = ~id, estimate = "mm", by = ~Party4)

diff_mms <- cj(na.omit(mydata), candidate_vote ~ Gender, id = ~id, estimate = "mm_diff",�
� by = ~Party4)

mms
diff_mms

## The difference in marginal means Rep - Dem is 5 percentage points, and it is significant, p<0.01. Levels are .48 Rep and .53 Dem.�






